package com.kcwl.carrier.infrastructure.repository.persostence;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kcwl.carrier.domain.repository.ICarrierCollectionRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.CarrierCollectionMapper;
import com.kcwl.carrier.infrastructure.repository.po.CarrierCollectionCompanyInfoPO;
import com.kcwl.carrier.interfaces.command.CarrierCompanyCommand;
import com.kcwl.carrier.interfaces.dto.CarrierCollectionCompanyInfoDTO;
import com.kcwl.carrier.interfaces.query.CarrierCompanyQuery;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CarrierCollectionRepositoryImpl extends BaseRepositoryImpl<CarrierCollectionMapper, CarrierCollectionCompanyInfoPO> implements ICarrierCollectionRepository {
    @Resource
    private CarrierCollectionMapper carrierCollectionMapper;
    @Override
    public CarrierCollectionCompanyInfoPO getCarrierCompany(CarrierCompanyQuery query) {
        LambdaUpdateWrapper<CarrierCollectionCompanyInfoPO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CarrierCollectionCompanyInfoPO::getUserId,query.getUserId())
                .eq(CarrierCollectionCompanyInfoPO::getDepartmentId,query.getDepartmentId());
        return carrierCollectionMapper.selectOne(lambdaUpdateWrapper);
    }

    @Override
    public int addCarrierCompany(CarrierCompanyCommand command) {
        return carrierCollectionMapper.insert(KcBeanConverter.toBean(command,CarrierCollectionCompanyInfoPO.class));
    }

    @Override
    public int updateCarrierCompany(CarrierCompanyCommand command) {
        return carrierCollectionMapper.updateById(KcBeanConverter.toBean(command,CarrierCollectionCompanyInfoPO.class));
    }
    @Override
    public List<CarrierCollectionCompanyInfoDTO> getCollectShippers(CarrierCompanyQuery query){
        LambdaUpdateWrapper<CarrierCollectionCompanyInfoPO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CarrierCollectionCompanyInfoPO::getUserId,query.getUserId()).eq(CarrierCollectionCompanyInfoPO::getIsDel, YesNoEnum.NAY.getValue());
        List<CarrierCollectionCompanyInfoPO> list = carrierCollectionMapper.selectList(wrapper);
        return  KcBeanConverter.toList(list,CarrierCollectionCompanyInfoDTO.class);
    }

    @Override
    public int getCollectCount(CarrierCompanyQuery query){
        LambdaUpdateWrapper<CarrierCollectionCompanyInfoPO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CarrierCollectionCompanyInfoPO::getEnterpriseId,query.getEnterpriseId()).eq(CarrierCollectionCompanyInfoPO::getIsDel, YesNoEnum.NAY.getValue())
                .eq(CarrierCollectionCompanyInfoPO::getDepartmentId, query.getDepartmentId());
        return carrierCollectionMapper.selectCount(wrapper);
    }
}
